package com.ymatou.productstock.api.testcase;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.productstock.api.parameter.CatalogBean;
import com.ymatou.productstock.api.service.SyncStockFromXloboCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.ProductStockCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtSyncStockLog;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.YmtSyncStockLogWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.JsonBeanFile;

public class Ts_SyncStockFromXlobo {
	//base data
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	//sql db
	private static YmtCatalogsIWapper ymtCatalogsIWapper=new YmtCatalogsIWapper();
	private static YmtSyncStockLogWapper ymtSyncStockLogWapper=new YmtSyncStockLogWapper();
	
	
	//API
	private static SyncStockFromXloboCall syncStockFromXloboCall=new SyncStockFromXloboCall();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海增量同步FBX库存信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncStockFromXloboCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}

	//贝海调用来同步FBX增量库存
	@Test
	@Category({P1.class})
	@TestCase
	@SuppressWarnings("unchecked")
	public void Tc_SyncStockFromXlobo_001() {
		Logger.start(true, "贝海增量同步FBX库存信息");
		try{

			//发布商品
			String skuStr = TestDataManager.getRandomNum(10);
			Map<String, Object> updateProduct = MapUtil.hashMap("Product->Catalogs[0]->SKU",skuStr);
			
			String productId = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, updateProduct).getJSONObject("Data").getString("ProductID");
			
			//修改catalogType=2 FBX
			new YmtProductsIWapper().updateCatalogTypeByProductId(productId, 2);
			ymtCatalogsIWapper.updateCatalogTypeByProductId(productId, 2);
			
			//获取数据
			Map<String, Object> retMap = ymtCatalogsIWapper.selectCatalogsByProductIdforMap(productId).get(0);
			int oNum = (int)retMap.get("iNum");
			String catalogsId = retMap.get("sCatalogId").toString();
			
			String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
			int updateNum = oNum+1;
			
			//贝海同步库存
			JSONObject req=JsonBeanHelper.getJsonBean(JsonBeanFile.SyncStockFromXloboTpl);
			Map<String,Object> updateMap = MapUtil.hashMap("UserID",sellerId,"FBXCatalogList[0]->SKU",skuStr,"FBXCatalogList[0]->Stock",updateNum,
															"FBXCatalogList[0]->SyncStockId",uuid);
	
			JsonBeanHelper.updateJsonBean(req, updateMap);
			syncStockFromXloboCall.setData(req);
			syncStockFromXloboCall.callService();			
			JSONObject retBean=new JSONObject(syncStockFromXloboCall.getReturnData());
			Logger.verifyEquals(200, retBean.getInt("Code"), "响应报文-返回码");
			
			Thread.sleep(5000);
			//check 
			//SQL Ymt_Catalogs
			YmtCatalogsI ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0);
			Logger.verifyEquals(updateNum, ymtCatalogsI.getiNum(), "SQL-Ymt_Catalogs 商品库存数量");
			
			//SQL Ymt_SyncStockLog
			YmtSyncStockLog ymtSyncStockLog = ymtSyncStockLogWapper.selectBySyncStockId(uuid);
			Map<String, Object> tgtRetMap = MapUtil.hashMap("iUserID", sellerId, "iStock", updateNum,"sSKU",skuStr);
			AssertService.assertResultEqual(tgtRetMap, MapUtil.pojoToMap(ymtSyncStockLog));

			//Check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				//Mongo-ProductStockInfo
				Logger.verifyEquals(updateNum,new ProductStockInfoWapper().getByCatalogId(catalogsId).get("StockNum"),"Mongo-ProductStockInfo 库存");
				
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				
				CatalogBean catalogBean = new CatalogBean();
				CatalogBean.StockBean stockBean =catalogBean.new StockBean();
				
				stockBean.setStock(updateNum);
				stockBean.setStockType(0);
				catalogBean.setCatalogId(catalogsId);
				catalogBean.setStockBean(Arrays.asList(stockBean));
				ProductStockCallService.checkNewMongoCatalogStock(productId, Arrays.asList(catalogBean));
				 
			}
			
			
			
			
			
		/*	//查询当前某SKU的库存数量
			//查询catalogs表，根据userid和Sku查询某用户某SKU的规格的库存数量
			YmtCatalogsIWapper ymtCatalogsIWapper=new YmtCatalogsIWapper();
			List <Map>sqlRet=ymtCatalogsIWapper.selectCatalogsByUserIdandSku(3383, "ceshi100");
			int oNum=(int) sqlRet.get(0).get("iNum");
			Logger.debug("推送之前的库存为："+oNum);

			//生成UUID，贝海推送做幂等印子
			JSONObject req=JsonBeanHelper.getJsonBean(JsonBeanFile.SyncStockFromXloboTpl);
			SyncStockFromXloboCall syncStockFromXloboCall=new SyncStockFromXloboCall();
			Map<String,Object>mm=new HashMap<>();
			mm.put("FBXCatalogList[0]->SKU", "ceshi100");
			mm.put("FBXCatalogList[0]->Stock", oNum+1);
			mm.put("FBXCatalogList[0]->SyncStockId", UUID.randomUUID().toString().replaceAll("\\-", ""));
			JsonBeanHelper.updateJsonBean(req, mm);
			syncStockFromXloboCall.setData(req);
			syncStockFromXloboCall.callService();			
			JSONObject ret=new JSONObject(syncStockFromXloboCall.getReturnData());
			//返回报文断言
			Logger.verifyNotNull(ret, "返回报文非空");
			//断言"Code":200
			Logger.verifyIsContains(ret.getString("Code"), 200, "\"Code\":200");
			//判断推送后库存数据是否加1
			YmtCatalogsIWapper ymtCatalogsIWapper2=new YmtCatalogsIWapper();
			List <Map>sqlRet2=ymtCatalogsIWapper2.selectCatalogsByUserIdandSku(3383, "ceshi100");
			Logger.verifyEquals(oNum+1,sqlRet2.get(0).get("iNum"), "判断推送后该SKU的库存数量有正确增加1个");
			//判断Ymt_SyncStockLog表中的推送记录
			JSONObject aa=GetProductInfoByProductIdCall.GetProductInfoByProductId("ProductId=cf4ebeb0-e6a4-4a83-a7cb-b6b0377073f3");
			Logger.debug(aa);*/
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	

	
	
	
	
}

